package com.sz.service;

import com.codingapi.txlcn.tc.annotation.DTXPropagation;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.sz.mapper.OnlineBankCardMapper;
import com.sz.model.OnlineBankCard;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class OnlineBankCardServiceImpl implements IOnlineBankCardService {

    @Autowired
    private OnlineBankCardMapper onlineBankCardMapper;

    @Override
    public List<OnlineBankCard> getAllBankCard(int obid) {
        return onlineBankCardMapper.getAllBankCard(obid);
    }

    @Override
    public OnlineBankCard getBankCard(String card) {
        return onlineBankCardMapper.getBankCard(card);
    }

    @Override
    public int insertBankCart(OnlineBankCard onlineBankCard) {
        return onlineBankCardMapper.insertBankCart(onlineBankCard);
    }

    @Override
    public Long selectMoney(String ba) {
        return onlineBankCardMapper.selectMoney(ba);
    }

    //网银转账
    @Transactional
    @Override
    public int onlineBankTransfer(String obca1, String obca2, Long tradingAmount) {
        Long obcBalance1 = onlineBankCardMapper.getBankCard(obca1).getObcBalance();
        Long obcBalance2 = onlineBankCardMapper.getBankCard(obca2).getObcBalance();

        int i = onlineBankCardMapper.updateOnlineBankMoney(obca1, obcBalance1 - tradingAmount);
        //System.out.println(1/0);
        i+=onlineBankCardMapper.updateOnlineBankMoney(obca2,obcBalance2+tradingAmount);

        return i;
    }

    @LcnTransaction(propagation = DTXPropagation.REQUIRED)
    @Transactional
    @Override
    public int RegTranBank(String acc, Long amount) {

        Long obcBalance1 = onlineBankCardMapper.getBankCard(acc).getObcBalance();
        return onlineBankCardMapper.updateOnlineBankMoney(acc,obcBalance1+amount);
    }


}
